package com.wangijun.yuan.web.service;


import cn.hutool.core.thread.ThreadUtil;
import com.wangijun.yuan.web.mapper.ArticleMapper;
import com.wangijun.yuan.web.mapper.CategoryMapper;
import com.wangijun.yuan.web.mapper.TagInfoMapper;
import com.wangijun.yuan.web.mapper.TopicInfoMapper;
import com.wangijun.yuan.web.model.vo.ArticleContentVO;
import com.wangijun.yuan.web.model.vo.TagInfoVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


@Slf4j
@Service
@RequiredArgsConstructor
public class ArtEventService {
    private final UserService userService;
    private final TopicInfoMapper topicInfoMapper;
    private final TagInfoMapper tagInfoMapper;
    private final CategoryMapper categoryMapper;
    private final ArticleMapper articleMapper;

    /**
     * 功能描述: 文章阅读数统计
     *
     * @author wlf 2024/8/1 11:22
     */
    public void read(ArticleContentVO vo) {
        ThreadUtil.execAsync(() -> {
            // 文章阅读数
            articleMapper.readNumAdd(vo.getId(), 1);
            // 标签阅读数
            for (TagInfoVO tagInfoVO : vo.getTagList()) {
                tagInfoMapper.readNumAdd(tagInfoVO.getId(), 1);
            }
            // 话题阅读数
            topicInfoMapper.readNumAdd(vo.getTopicId(), 1);
            // 用户阅读数
            userService.getBaseMapper().readNumAdd(vo.getCreateBy(), 1);
            userService.cacheEvict(vo.getCreateBy());
            // 分类文章总数
            categoryMapper.readNumAdd(vo.getCategoryId(), 1);
        });

    }
}
